package com.cgtong.android.mvp.model;

import com.cgtong.android.bean.ProblemBean;
import com.cgtong.android.http.ApiManager;
import com.cgtong.android.http.callback.BaseCallBack;
import com.cgtong.android.http.callback.FollowCallBack;
import com.cgtong.android.http.callback.ProblemPageCallBack;
import com.cgtong.android.mvp.contract.ProblemPageContract;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.CallBackProxy;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.ApiResult;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * Created by chunc on 2017/10/23.
 */

public class ProblemPageModel implements ProblemPageContract.Model {

    @Override
    public void httpProblemsData(String maxTime, String searchTxt, int isDraft, int status, int isExpired, String latitude, String longitude, String beginTime,String endTime,final ProblemPageCallBack<List<ProblemBean>> problemPageCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_PAGE_URL)
                .params("searchtxt", searchTxt)//支持添加多个参数同时添加
                .params("maxtime", maxTime)//支持添加多个参数同时添加
                .params("isdraft", String.valueOf(isDraft))//支持添加多个参数同时添加
                .params("status", String.valueOf(status))//支持添加多个参数同时添加
                .params("isexpired", String.valueOf(isExpired))//支持添加多个参数同时添加
                .params("lat", latitude)//支持添加多个参数同时添加
                .params("lng", longitude)//支持添加多个参数同时添加
                .params("begintime",beginTime)
                .params("endtime",endTime)
                .accessToken(true)
                .execute(new CallBackProxy<ApiResult<List<ProblemBean>>, List<ProblemBean>>(new SimpleCallBack<List<ProblemBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        problemPageCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(List<ProblemBean> problemBeen) {
                        problemPageCallBack.onHttpProblemsSuccess(problemBeen);
                    }
                }) {
                });
    }


    @Override
    public void follow(final long id, final FollowCallBack<ApiResult> followCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_FOLLOW_USER_URL)
                .params("userid", String.valueOf(id))//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        followCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            ApiResult result = new ApiResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            followCallBack.onFollowUserSuccess(id, result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    @Override
    public void httpMyDrafts( final ProblemPageCallBack<List<ProblemBean>> problemPageCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_PAGE_URL)
                .params("searchtxt", "")//支持添加多个参数同时添加
                .params("maxtime", "")//支持添加多个参数同时添加
                .params("isdraft", "1")//支持添加多个参数同时添加
                .params("status", "0")//支持添加多个参数同时添加
                .params("isexpired", "0")//支持添加多个参数同时添加
                .params("lat", "null")//支持添加多个参数同时添加
                .params("lng", "null")//支持添加多个参数同时添加
                .accessToken(true)
                .execute(new CallBackProxy<ApiResult<List<ProblemBean>>, List<ProblemBean>>(new SimpleCallBack<List<ProblemBean>>() {
                    @Override
                    public void onError(ApiException e) {
                        problemPageCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(List<ProblemBean> problemBeen) {
                        problemPageCallBack.onHttpProblemsSuccess(problemBeen);
                    }
                }) {
                });
    }

    @Override
    public void deleteDraft(long id, final BaseCallBack<ApiResult> baseCallBack) {
        EasyHttp.post(ApiManager.PROBLEM_DELDRAFT_URL)
                .params("problemid", String.valueOf(id))//支持添加多个参数同时添加
                .accessToken(true)//本次请求是否追加token
                .execute(new SimpleCallBack<String>() {

                    @Override
                    public void onError(ApiException e) {
                        baseCallBack.onError(e);
                    }

                    @Override
                    public void onSuccess(String apiResult) {
                        try {
                            JSONObject object = new JSONObject(apiResult);
                            ApiResult result = new ApiResult();
                            result.setCode(object.optInt("code"));
                            result.setMsg(object.optString("msg"));
                            baseCallBack.onSuccess(result);
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
